﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class RouteFlight_Entity : EntityBase<ROUTEFLIGHT>
    {
        public override RESP_Info Add(ROUTEFLIGHT model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into ROUTE_FLIGHT(ROUTE_ID,ROUTE_LUGGAGE_CD,ROUTE_LUGGAGE_BJ,ROUTE_TAKEOFF_CD,ROUTE_TAKEOFF_BJ,ROUTE_LANDING_CD,ROUTE_LANDING_BJ,ROUTE_ELSE) values (@ROUTE_ID,@ROUTE_LUGGAGE_CD,@ROUTE_LUGGAGE_BJ,@ROUTE_TAKEOFF_CD,@ROUTE_TAKEOFF_BJ,@ROUTE_LANDING_CD,@ROUTE_LANDING_BJ,@ROUTE_ELSE)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("ROUTE_ID", model.ROUTE_ID));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_LUGGAGE_CD", model.ROUTE_LUGGAGE_CD));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_LUGGAGE_BJ", model.ROUTE_LUGGAGE_BJ));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_TAKEOFF_CD", model.ROUTE_TAKEOFF_CD));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_TAKEOFF_BJ", model.ROUTE_TAKEOFF_BJ));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_LANDING_CD", model.ROUTE_LANDING_CD));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_LANDING_BJ", model.ROUTE_LANDING_BJ));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_ELSE", model.ROUTE_ELSE));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加航班成功</p></h5>";
                    info.Url = "RouteList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加航班失败</p></h5>";
                    info.Url = "RouteFlightAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加航班失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(ROUTEFLIGHT model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update ROUTE_FLIGHT set ROUTE_LUGGAGE_CD=@ROUTE_LUGGAGE_CD,ROUTE_LUGGAGE_BJ=@ROUTE_LUGGAGE_BJ,ROUTE_TAKEOFF_CD=@ROUTE_TAKEOFF_CD,ROUTE_TAKEOFF_BJ=@ROUTE_TAKEOFF_BJ,ROUTE_LANDING_CD=@ROUTE_LANDING_CD,ROUTE_LANDING_BJ=@ROUTE_LANDING_BJ,ROUTE_ELSE=@ROUTE_ELSE where ROUTE_ID=@ROUTE_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("ROUTE_ID", model.ROUTE_ID));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_LUGGAGE_CD", model.ROUTE_LUGGAGE_CD));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_LUGGAGE_BJ", model.ROUTE_LUGGAGE_BJ));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_TAKEOFF_CD", model.ROUTE_TAKEOFF_CD));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_TAKEOFF_BJ", model.ROUTE_TAKEOFF_BJ));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_LANDING_CD", model.ROUTE_LANDING_CD));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_LANDING_BJ", model.ROUTE_LANDING_BJ));
                    cmd.Parameters.Add(new SqlParameter("ROUTE_ELSE", model.ROUTE_ELSE));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑航班成功</p></h5>";
                    info.Url = "RouteList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑航班失败</p></h5>";
                    info.Url = "RouteFlightAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑航班失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(ROUTEFLIGHT hotel)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from ROUTE_FLIGHT where ROUTE_ID=@ROUTE_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("ROUTE_ID", hotel.ROUTE_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除航班成功</p></h5>";
                    info.Url = "RouteList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除航班失败</p></h5>";
                    info.Url = "RouteList.aspx";

                    Tom.Net.Log.LogManager.Error("删除航班失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override ROUTEFLIGHT GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select ROUTE_ID,ROUTE_LUGGAGE_CD,ROUTE_LUGGAGE_BJ,ROUTE_TAKEOFF_CD,ROUTE_TAKEOFF_BJ,ROUTE_LANDING_CD,ROUTE_LANDING_BJ,ROUTE_ELSE from ROUTE_FLIGHT where ROUTE_ID=@ROUTE_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("ROUTE_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        ROUTEFLIGHT hotel = new ROUTEFLIGHT();
                        if (!reader.IsDBNull(0)) hotel.ROUTE_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) hotel.ROUTE_LUGGAGE_CD = reader.GetString(1);
                        if (!reader.IsDBNull(2)) hotel.ROUTE_LUGGAGE_BJ = reader.GetString(2);
                        if (!reader.IsDBNull(3)) hotel.ROUTE_TAKEOFF_CD = reader.GetString(3);
                        if (!reader.IsDBNull(4)) hotel.ROUTE_TAKEOFF_BJ = reader.GetString(4);
                        if (!reader.IsDBNull(5)) hotel.ROUTE_LANDING_CD = reader.GetString(5);
                        if (!reader.IsDBNull(6)) hotel.ROUTE_LANDING_BJ = reader.GetString(6);
                        if (!reader.IsDBNull(7)) hotel.ROUTE_ELSE = reader.GetString(7);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return hotel;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取航班数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override ROUTEFLIGHT GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        ROUTEFLIGHT hotel = new ROUTEFLIGHT();
                        if (!reader.IsDBNull(0)) hotel.ROUTE_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) hotel.ROUTE_LUGGAGE_CD = reader.GetString(1);
                        if (!reader.IsDBNull(2)) hotel.ROUTE_LUGGAGE_BJ = reader.GetString(2);
                        if (!reader.IsDBNull(3)) hotel.ROUTE_TAKEOFF_CD = reader.GetString(3);
                        if (!reader.IsDBNull(4)) hotel.ROUTE_TAKEOFF_BJ = reader.GetString(4);
                        if (!reader.IsDBNull(5)) hotel.ROUTE_LANDING_CD = reader.GetString(5);
                        if (!reader.IsDBNull(6)) hotel.ROUTE_LANDING_BJ = reader.GetString(6);
                        if (!reader.IsDBNull(7)) hotel.ROUTE_ELSE = reader.GetString(7);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return hotel;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取航班数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<ROUTEFLIGHT> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<ROUTEFLIGHT> hotels = new List<ROUTEFLIGHT>();
                    while (reader.Read())
                    {
                        ROUTEFLIGHT hotel = new ROUTEFLIGHT();
                        if (!reader.IsDBNull(0)) hotel.ROUTE_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) hotel.ROUTE_LUGGAGE_CD = reader.GetString(1);
                        if (!reader.IsDBNull(2)) hotel.ROUTE_LUGGAGE_BJ = reader.GetString(2);
                        if (!reader.IsDBNull(3)) hotel.ROUTE_TAKEOFF_CD = reader.GetString(3);
                        if (!reader.IsDBNull(4)) hotel.ROUTE_TAKEOFF_BJ = reader.GetString(4);
                        if (!reader.IsDBNull(5)) hotel.ROUTE_LANDING_CD = reader.GetString(5);
                        if (!reader.IsDBNull(6)) hotel.ROUTE_LANDING_BJ = reader.GetString(6);
                        if (!reader.IsDBNull(7)) hotel.ROUTE_ELSE = reader.GetString(7);

                        hotels.Add(hotel);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return hotels;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取航班数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}
